package org.xydpeng.framework.l2cache.properties;

import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.xydpeng.framework.l2cache.enums.RedisDataType;

import java.time.Duration;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;

/**
 * @author xydpeng
 * @version v1.0
 * @date: 2024/6/6
 * @description:
 */
@Getter
@Setter
@ConfigurationProperties(prefix = "spring.cache.l2cache")
public class L2CacheConfigProperties {
    static final int UNSET_INT = -1;
    final long instanceId = UUID.randomUUID().getLeastSignificantBits();

    boolean enable = false;

    String syncTopic;

    long delayMillis = 6000;

    int delayThreads = 1;

    String Serialization = "kryo";

    List<String> cacheNames = new ArrayList<>();

    Map<String, L2CacheProperties> caches = new HashMap<>();

    public L2CacheProperties getConfig(String cacheName) {
        return Objects.isNull(caches) ? null : caches.get(cacheName);
    }


    @Setter
    @Getter
    public static class L2CacheProperties {
        String cacheName;
        Boolean cacheNullValues = true;
        /*   =====  caffine 相关配置 =====  */
        long maximumSize = UNSET_INT;
        int initialCapacity = UNSET_INT;
        long expireAfterWriteNanos = UNSET_INT;
        long expireAfterAccessNanos = UNSET_INT;
        long refreshAfterWriteNanos = UNSET_INT;

        /*   =====  redis 相关配置 =====  */
        /**
         * Entry expiration. By default the entries never expire.
         */
        Duration timeToLive;

        RedisDataType redisDataType = RedisDataType.STRING;
        /**
         * Key prefix.
         */
        String keyPrefix;
        /**
         * Whether to use the key prefix when writing to Redis.
         */
        boolean useKeyPrefix;

    }

}
